package com.cictec.middleware.gps.engine.position.handler.alarms;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.PropertiesUtils;
import com.cictec.middleware.core.entity.DeparterSchedule;
import com.cictec.middleware.core.entity.WarnMsg;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.exception.BusPositionWarnHandlerException;
import com.cictec.middleware.gps.engine.position.handler.BusPositionAlarmsHandler;
import com.cictec.middleware.gps.service.ScheduleService;
import com.cictec.middleware.gps.service.WarnMsgService;

/**
 * 提前发车报警
 * @author ql
 * @date 2017年9月26日
 */
@Component
public class DepartAdvanceWarnHandler implements WarnHandler {


	private static Logger logger = LoggerFactory.getLogger(BusPositionAlarmsHandler.class);
	@Autowired
	private WarnMsgService warnMsgService;
	@Autowired
	private ScheduleService scheduleService;
	
	
	@Override
	public String getWarnType() {
		return Constants.VAL_BUS_STATUS_11;
	}
	
	@Override
	public void onHandler(BusInstance busInstance) throws BusPositionWarnHandlerException {
		logger.debug("【提前发车报警信息处理】");
		if(!busInstance.isEnterOut()){
			return;
		}
		Date currentTime = busInstance.getTime();
		DeparterSchedule ds = scheduleService.getDeparterSchedule(busInstance.getLineUuid(), busInstance.getLineType(), busInstance.getBusUuid(), currentTime, 0, false);
		if(ds == null){
			return;
		}
		
		int advanceSeconds = PropertiesUtils.getIntValue("system.parameter.warn.advanceSeconds", 60*5);
		if(ds.getDepartureTime().getTime() > currentTime.getTime()){
			long cha = DateUtils.secondInterval(ds.getDepartureTime(), currentTime);
			
			String seq = busInstance.isEnterOut() ? busInstance.getStationLineSeqOrg() : "";
			// 判断第一站进出站情况
			if(cha > advanceSeconds && seq.equals("1")){
				// 提前发车大于设定阀值
				// 记录提前发车
				startWarn(busInstance);
			}else{
				stopWarn(busInstance);
			}
		}else if(currentTime.getTime() > ds.getDepartureTime().getTime()){
			stopWarn(busInstance);
		}
		
		// TODO Auto-generated method stub
	}
	
	public void startWarn(BusInstance busInstance){
		WarnMsg warnMsg = busInstance.getWarnsByWarnType(Constants.VAL_BUS_STATUS_11);
		if(warnMsg == null){
			warnMsg = new WarnMsg();
			warnMsg.setWarnBusUuid(busInstance.getBusUuid());
			warnMsg.setWarnDevUuid(busInstance.getDevCode());

			if(!PMSUtils.isEmpty(busInstance.getDriverUuid())){
				warnMsg.setWarnDrvUuid(busInstance.getDriverUuid());
			}
			warnMsg.setWarnLineUuid(busInstance.getLineUuid());
			warnMsg.setWarnStartTime(DateUtils.formatDateTimes(busInstance.getTime()));
			// 提前发车
			warnMsg.setWarnType(Constants.VAL_BUS_STATUS_11);
			logger.debug("【提前发车】");
			warnMsg.setWarnStorageTime(DateUtils.formatDateTimes(new Date()));
			
			warnMsgService.saveWarn(warnMsg);
			
			busInstance.setWarns(Constants.VAL_BUS_STATUS_11, warnMsg);
		}
	}

	@Override
	public void stopWarn(BusInstance busInstance) {
		// TODO Auto-generated method stub
		WarnMsg warnMsg = busInstance.getWarnsByWarnType(Constants.VAL_BUS_STATUS_11);
		if(warnMsg != null){
			warnMsg.setWarnEndTime(DateUtils.formatDateTimes(busInstance.getTime()));
			warnMsgService.saveWarn(warnMsg);
			
			busInstance.setWarns(Constants.VAL_BUS_STATUS_11, null);
		}
	}

}
